﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SARD.Aux_Forms
{
    public partial class convolutionkernel : Form
    {
        int[,] matrix;

        string smatrix;

        /// <summary>
        /// Initializes the form interface.
        /// </summary>
        public convolutionkernel()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Sends the kernel to the main window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UI_Click(object sender, EventArgs e)
        {
            smatrix = this.textBox1.Text;

            string[] numbers;
            bool erro = false;
            string[] split = smatrix.Split('\n');
            
            numbers = split[0].Split(',');
            int size = numbers.Length;

            
            //Checks the size an length of the matrix.
            if ((size % 2 == 0) || (split.Length % 2 == 0))
            {
                MessageBox.Show("Tamanho inválido, as linhas e colunas devem ter tamanho impar", "Erro");
                erro = true;
            }

            int i, j;
            i = j = 0;
            matrix = new int[split.Length, size];


            if(erro == false)
                foreach (String line in split)
                {
                    j = 0;
                    numbers = line.Split(',');

                    if (numbers.Length != size)
                    {
                        MessageBox.Show("Erro as linhas tem tamanhos diferentes", "Erro");
                        erro = true;
                        break;
                    }
                    else
                        foreach(String num in numbers)
                        {
                            matrix[i, j] = Convert.ToInt32(num);
                            j++;
                        }

                    i++;

                }


            if (erro == false)
                this.Close();
        }


        /// <summary>
        /// Returns the kernel to the main window
        /// </summary>
        /// <returns></returns>
        public int[,] returnmatrix()
        {
            return matrix;
        }

        /// <summary>
        /// close the form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
